package yxy.game.pm2.active.command.bottlewish;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actwish;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_心愿瓶;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BottleWishMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/****************************************
 * @name CMD_Active_心愿瓶_BottleWishReward
 * @description 心愿瓶活动许愿奖励领取
 * @author 梁健彬
 * @since 2022-08-22
 ***************************************/
@MessageType(proto = Command.BottleWishReward, classtype = BottleWishMessage.BottleWishRewardRequest.class)
public class CMD_Active_心愿瓶_BottleWishReward extends Command<BottleWishMessage.BottleWishRewardRequest> {

    @Override
    public void execute(MessageSource messageSource, BottleWishMessage.BottleWishRewardRequest request) throws Exception {
        String playercode = messageSource.getTag();
        int bottleId = request.getBottleId();//瓶子id
        BottleWishMessage.BottleWishRewardResponse.Builder response = BottleWishMessage.BottleWishRewardResponse.newBuilder();
        ActiveModule_心愿瓶 am = ActiveModule_心愿瓶.get(playercode, false);
        try {

            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            actwish data = DataCenter.getData(bottleId, actwish.class);
            if (data == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            HashMap<Integer, Integer> bottle = am.getBottle();
            if (bottle.get(bottleId) < data.getConversionScore()) {
                send(messageSource, response, Result.BottleWishCantReward);
                return;
            }

            //todo 领奖
            CurrencyHandle currencyHandle = new CurrencyHandle();

            if (am.tryLock()) {
                bottle.put(bottleId, bottle.get(bottleId) - data.getConversionScore());
                Currencies curs = new Currencies(Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum()));
                Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
                currencyHandle.addCurrency(playercode, CCReason.心愿瓶, curs);
                response.addAllItem(items);
                am.set();
            }

            //todo 瓶子的信息
            Map<String, actwish> allData = DataCenter.getAllData(actwish.class);
            for (actwish value : allData.values()) {
                BottleWishMessage.BottleMsg.Builder builder = BottleWishMessage.BottleMsg.newBuilder();
                builder.setDataId(value.getId());
                builder.setValue(bottle.get(value.getId()));
                builder.setCanRewardValue(value.getConversionScore());
                Currencies curs = new Currencies(Currency.create(value.getRewardtype(), value.getRewardid(), value.getRewardnum()));
                Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
                builder.addAllItem(items);
                response.addBottles(builder);
            }

            //todo 大奖日志
            PlayerBean pb = PlayerBean.get(playercode);
            ServerBean sb = ServerBean.get(pb.getServerId());
            response.addAllAwardsInfo(am.getLog(sb.getAreaId()));

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        } finally {
            am.unlock();
        }
    }


    private void send(MessageSource messageSource, BottleWishMessage.BottleWishRewardResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

}
